package com.atguigu.blog.controller;

import com.atguigu.blog.pojo.Article;
import com.atguigu.blog.service.ArticleService;
import com.atguigu.blog.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author 孤妤
 * @Date 2023/11/24 16:50
 */
@RestController
@RequestMapping("article")
@CrossOrigin
@Tag(name = "文章模块")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @PostMapping("showAll")
    @Operation(summary = "查询文章分类列表")
    @Parameters(@Parameter(name = "article",description = "文章对象",required = true))
    public Result<List<Article>> showAll(@RequestBody Article article){
        List<Article> articleList = articleService.showAll(article);
        return Result.ok(articleList);
    }
    @GetMapping("{id}")
    @Operation(summary = "查询文章详情")
    @Parameters(@Parameter(name = "id",description = "文章详情id",required = true))
    public Result<Article> showAllById(@PathVariable Integer id){
        Article article = articleService.showAllById(id);
        return Result.ok(article);
    }
    @GetMapping("show/{uid}")
    @Operation(summary = "根据用户id查询所有文章")
    @Parameters(@Parameter(name = "uid",description = "用户id",required = true))
    public Result<List<Article>> showAllByUid(@PathVariable Integer uid){
        List<Article> articleList = articleService.showAllByUid(uid);
        return Result.ok(articleList);
    }
    @PostMapping
    @Operation(summary = "添加文章信息")
    @Parameters(@Parameter(name = "article",description = "添加文章的对象",required = true))
    public Result<Integer> add(@RequestBody Article article){
        Integer result = articleService.add(article);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
    @DeleteMapping("{id}")
    @Operation(summary = "删除文章")
    @Parameters(@Parameter(name = "id",description = "删除文章的id",required = true))
    public Result<Integer> remove(@PathVariable Integer id){
        Integer result = articleService.remove(id);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
    @PutMapping
    @Operation(summary = "修改文章")
    @Parameters(@Parameter(name = "article",description = "修改文章的对象",required = true))
    public Result<Integer> remove(@RequestBody Article article){
        Integer result = articleService.change(article);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
}